package com.bulk.idCreate;

import cn.hutool.core.util.IdUtil;
import lombok.SneakyThrows;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * id生成测试
 *
 * @author 散装java
 */
public class IdCreateTest {
    @SneakyThrows
    public static void main(String[] args) {
        HashSet<Long> longs = new HashSet<>();
        Collection<Long> longs1 = Collections.synchronizedCollection(longs);
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        longs1.add(1L);
        longs1.add(1L);
        for (int i = 0; i < 1000; i++) {
            AtomicInteger atomicInteger = new AtomicInteger(i);
            new Thread(() -> {
                System.out.println(atomicInteger.get());
                long snowflakeNextId = IdUtil.getSnowflakeNextId();
                longs1.add(snowflakeNextId);
                System.out.println(snowflakeNextId);
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.println("size ==" + longs1.size());
//        System.out.println(JSONUtil.toJsonStr(longs1));
    }
}
